local runlua = {}

local resxml = [[
    <Res>
        <Font name="sf-12" source="fonts/simfang.ttf" size="12"/>
        <Font name="time" source="fonts/simfang.ttf" size="18"/>


        <Image name="logo" source="images/icon/32X32.ico"/>
        <Image name="close" source="images/close.png"/>
        <Image name="min" source="images/min.png"/>
        <Image name="button" source="images/btn.png"/>
        <Image name="edit" source="images/loginedit.png"/>


        <Cursor name="hand" sys="hand"/>
        <Cursor name="ibeam" sys="ibeam"/>

    </Res>
]]

local wndxml = [[
    <Window title="test" size="320,400" caption="0,0,0,30">
    <VerticalLayout>
        <HorizontalLayout height="32" color="dst:bk;color:#FFFFFFFF;">
            <Control size="40,32" image="dst:bk;image:(img:logo;center:true;);"/>
            <Label name="time" font="time"/>
            <Button name="min" size="30,25" statusimage="dst:(normal,hot,pressed);status:(img:min;size:30,25;num:3;type:v1;);"/>
            <Button name="close" size="30,25" statusimage="dst:(normal,hot,pressed);status:(img:close;size:30,25;num:3;type:v1;);"/>
        </HorizontalLayout>
        
        <VerticalLayout inset="10,10,10,10" childpadding="10">
            <Text name="text" textpadding="5,5,5,5" color="dst:bk;color:#FFFFFFFF;"/>
            <HorizontalLayout height="25">
                <Button name="clearlua" text="清空脚本" height="25" pressedoffset="1,1" statusimage="dst:(normal,hot,pressed);status:(img:button;size:45,25;num:3;type:v1;cor:10,0,10,0;);"/>
                <Button name="clear" text="清空输出" height="25" pressedoffset="1,1" statusimage="dst:(normal,hot,pressed);status:(img:button;size:45,25;num:3;type:v1;cor:10,0,10,0;);"/>
            </HorizontalLayout>
            <Edit name="edit" height="35" cursor="ibeam" tiptext="请输入lua脚本" textpadding="5,0,5,0" statusimage="dst:(normal,(hot,pressed));status:(img:edit;src:0,0,54,16;size:27,16;num:2;type:h1;cor:5,5,5,5;);"/>
            <Button name="dolua" text="运行脚本" height="25" pressedoffset="1,1" statusimage="dst:(normal,hot,pressed);status:(img:button;size:45,25;num:3;type:v1;cor:10,0,10,0;);"/>
        </VerticalLayout>
    </VerticalLayout>
    </Window>
]]

function runlua:show(name)
    if not name then
        print("you should give a name.")
        return
    end

    if (not EDUI.Engine.LoadResFromStr(resxml)) then
        print("load res error.mabe it loaded.")
    end

    local wnd = EDUI.Engine.FindWindow(name)
    if not wnd then
        wnd = EDUI.Engine.LoadWindowFromStr(wndxml)
        if not wnd then
            print("load wnd error.")
            return
        end

        if (not wnd:SetName(name)) then
            print("give another name, please.")
            wnd:Close()
            return
        end

        local icon = EDUI.Engine.FindSurface("logo")
        wnd:SetIcon(icon)

        local minbtn = wnd:FindControl("min")
        if (minbtn) then
            minbtn:SetEvent("click", function(button)
                local wnd = button:GetWindow()
                if wnd then
                    wnd:MinWindow()
                end
            end)
        end

        local closebtn = wnd:FindControl("close")
        if (closebtn) then
            closebtn:SetEvent("click", function(button)
                local wnd = button:GetWindow()
                if wnd then
                    wnd:Close()
                end
            end)
        end

        wnd:AddTimer(500, function(id, interval, wnd)
            local time = wnd:FindControl("time")
            if time then
                time:SetText(os.date())
            end
            return interval
        end)

        local doluabtn = wnd:FindControl("dolua")
        if doluabtn then
            doluabtn:SetEvent("click", function(button)
                local wnd = button:GetWindow()
                runlua:dolua(wnd)
            end)
        end

        local clearbtn = wnd:FindControl("clear")
        if clearbtn then
            clearbtn:SetEvent("click", function(button)
                local wnd = button:GetWindow()
                runlua:clearText(wnd)
            end)
        end

        local clearluabtn = wnd:FindControl("clearlua")
        if clearluabtn then
            clearluabtn:SetEvent("click", function(button)
                local wnd = button:GetWindow()
                runlua:clearEdit(wnd)
            end)
        end

        local edit = wnd:FindControl("edit")
        if edit then
            edit:SetEvent("onEnter", function(edit)
                local wnd = edit:GetWindow()
                runlua:dolua(wnd)
            end)
        end
    end
    runlua.name = name

    local edit = wnd:FindControl("edit")
    if edit then
        edit:SetFocus()
    end

    wnd:ShowWindow()
end

function runlua:dolua(wnd)
    if not wnd then return end
    local edit = wnd:FindControl("edit")
    if edit then
        loadstring(edit:GetText())()
        edit:SetFocus()
    end
end

function runlua:clearText(wnd)
    if not wnd then return end
    local text = wnd:FindControl("text")
    if text then
        text:SetText("")
    end
    local edit = wnd:FindControl("edit")
    if edit then
        edit:SetFocus()
    end
end

function runlua:clearEdit(wnd)
    if not wnd then return end
    local edit = wnd:FindControl("edit")
    if edit then
        edit:SetText("")
        edit:SetFocus()
    end
end

runlua.print = print

function print(str)
    local wnd = EDUI.Engine.FindWindow(runlua.name)
    if not wnd then return end
    local textctrl = wnd:FindControl("text")
    if not textctrl then return end
    textctrl:SetText(textctrl:GetText() .. tostring(str))
end

return runlua